using IGeekFan.AspNetCore.Knife4jUI;
using Medical.Api.Write.AutoMapper;
using Medical.Api.Write.Service.RBAC;
using Medical.Api.Write.Service.WmsService;
using Medical.Interstructrue;
using Medical.Interstructrue.Base;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using System.Reflection;
using Yitter.IdGenerator;
using System.Text.Json;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers().AddJsonOptions(options =>
{
    // 配置JSON序列化选项
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    // 允许处理日期时间格式
    options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x =>
{
    var path= Path.Combine(AppContext.BaseDirectory, "Medical.Api.Write.xml");
    x.IncludeXmlComments(path,true);
});
// ���ݿ�����
builder.Services.AddDbContext<MedicalDbContext>(x =>
{
    x.UseMySql(builder.Configuration.GetConnectionString("MySQL"), new MySqlServerVersion("5.7"));
});
//MediatR
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
// ��־
builder.Host.UseNLog();
// ѩ���㷨
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());
// �Զ�ӳ��
builder.Services.AddAutoMapper(x =>
{
    x.AddProfile<BasicManagementMapper>();
    x.AddProfile<DepartmentManagementMapper>();
    x.AddProfile<InventoryManagementMapper>();
    x.AddProfile<PackagingSterilizationMapper>();
    x.AddProfile<RecyclingCleaningMapper>();
    x.AddProfile<WmsMapper>();
});
// �ִ�
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
//����
builder.Services.AddScoped(typeof(IRBACService), typeof(RBACService));
builder.Services.AddScoped(typeof(IWmsService), typeof(WmsService));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseKnife4UI();
    app.UseSwaggerUI();
}
// CORS
app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

// ��̬�ļ�
app.UseStaticFiles();
app.UseAuthorization();

app.MapControllers();

app.Run();

/// <summary>
/// 自定义的日期时间JSON转换器
/// </summary>
public class DateTimeConverter : JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.String)
        {
            string value = reader.GetString();
            if (DateTime.TryParse(value, out DateTime dateTime))
            {
                return dateTime;
            }
        }
        // 如果解析失败，返回当前时间
        return DateTime.Now;
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ss"));
    }
}
